// pages/learn/learn.js
import api from '../../config/api'
import { shareOptions } from '../../utils/access'
import request from '../../utils/request'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    loading:true,
    page: 1,
    total: 0,
    isDone: false,
    itemImages:[
    'https://image-classify.daieco.com/kejian-quanzi/',
    '7HtsxFEDChj3a200488c29a36f2853615db2af488597.png',
    'icUNAE96u1742b2b5629ab7336501c960caf7a8fd727.png',
    'fg3VKkt7BXZc52df7ccbb2907e7c3cfdb3fcc6fcdd82.png',
    'XIRraca3kecLbd0d670bf419b9f1d69b248a328f2fdf.png',
    'f5sz9JQvAzlc56cc37a0769f0442f0059c990a8e07b1.png'
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList(1)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 获取列表
   * @param {*} page 当前页
   */
  async getList(page) {
    const res = await request.get(api.knowledgeList, {
      page,
      size: 10
    }, { loading: false })
    this.setData({
      list: page === 1 ? (res?.data?.records || []) : [
        ...this.data.list,
        ...res?.data?.records,
      ],
      total: res?.data?.pages,
      loading:false
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      isDone: false,
    })
    this.getList(1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { page, total } = this.data
    if (page < total) {
      page++
      this.setData({
        page
      })
      this.getList(page)
    } else {
      this.setData({
        isDone: true
      })
    }
  },

  /**
   * 跳转详情页
   * @param {*} e 
   */
  jumpDetail(e) {
    const { dataset: { id = '', title = '' } } = e.currentTarget
    wx.navigateTo({
      url: `/pages/list/list?id=${id}&title=${title}`,
    })
  },

  /**
   * 跳转导航详情页
   */
  jumpBannerDetail() {
    wx.navigateTo({
      url: '/pages/detail/detail?id=33',
    })
  },

  // 用户点击右上角分享
	onShareAppMessage(e) {
		return shareOptions('default', e)
	},
})